package com.hongyi.system.service.impl;

import com.hongyi.common.framework.constant.Constants;
import com.hongyi.common.saToken.utils.LoginUtils;
import com.hongyi.system.domain.SysUserNotice;
import com.hongyi.system.domain.dto.SysUserNoticeDto;
import com.hongyi.system.domain.vo.SysUserNoticeVo;
import com.hongyi.system.mapper.SystemUserNoticeMapper;
import com.hongyi.system.service.ISysUserNoticeService;
import org.springframework.stereotype.Service;

import com.hongyi.common.mybatis.page.PageData;
import com.hongyi.common.mybatis.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hongyi.common.framework.utils.bean.MapstructUtils;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 用户消息关联Service业务层处理
 * 
 * @author DingHao
 * @date 2025-01-24 13:13:15
 */
@Service
public class SysUserNoticeServiceImpl implements ISysUserNoticeService {



    @Autowired
    private SystemUserNoticeMapper systemUserNoticeMapper;

    /**
     * 构建查询条件
     *
     */
    private LambdaQueryWrapper<SysUserNotice> buildQueryWrapper(SysUserNoticeDto dto) {
        Map<String, Object> params = dto.getParams();
        LambdaQueryWrapper<SysUserNotice> lqw = Wrappers.lambdaQuery();
        return lqw;
    }

    /**
     * 查询当前用户未读通知数量
     * @return 未读通知数量
     */
    @Override
    public long queryMyNoticeCount() {
        return systemUserNoticeMapper.selectCount(new LambdaQueryWrapper<SysUserNotice>()
                .eq(SysUserNotice::getUserId, LoginUtils.getUserId())
                .eq(SysUserNotice::getReadStatus, Constants.UN_READ)
        );

    }

    /**
     * 查询当前用户未读通知列表
     * @return 前3个未读通知列表
     */
    @Override
    public List<SysUserNoticeVo> queryMyNoticeList() {
        Long userId = LoginUtils.getUserId();
        List<SysUserNoticeVo> systemUserNoticeVos = systemUserNoticeMapper.selectMyNoticeList(userId, Constants.UN_READ);
        return systemUserNoticeVos.stream()
                .limit(3)
                .collect(Collectors.toList());
    }

    /**
     * 查询用户消息关联
     * 
     * @param id 用户消息关联ID
     * @return 用户消息关联
     */
    @Override
    public SysUserNoticeVo querySystemUserNoticeVoById(Long id) {
        SysUserNoticeVo sysUserNoticeVo = systemUserNoticeMapper.selectMyNoticeById(id);
        if (sysUserNoticeVo.getReadStatus().equals(Constants.UN_READ)) {
            SysUserNotice sysUserNotice = new SysUserNotice();
            sysUserNotice.setReadStatus(Constants.READ);
            sysUserNotice.setId(id);
            systemUserNoticeMapper.updateById(sysUserNotice);
        }
        return sysUserNoticeVo;
    }

    /**
     * 查询用户消息关联分页列表
     *
     * @param systemUserNoticeDto 用户消息关联业务对象
     * @param pageQuery 分页数据
     * @return 用户消息关联集合
     */
    @Override
    public PageData<SysUserNoticeVo> querySystemUserNoticeVoPageList(SysUserNoticeDto systemUserNoticeDto, PageQuery pageQuery) {
        systemUserNoticeDto.setUserId(LoginUtils.getUserId());
        Page<SysUserNoticeVo> result = systemUserNoticeMapper.selectMyNoticeListPage(systemUserNoticeDto, pageQuery.build());
        return PageData.success(result);
    }

    /**
     * 查询用户消息关联列表
     */
    @Override
    public List<SysUserNoticeVo> querySystemUserNoticeVoList(SysUserNoticeDto systemUserNoticeDto) {
        LambdaQueryWrapper<SysUserNotice> lqw = buildQueryWrapper(systemUserNoticeDto);
        return systemUserNoticeMapper.selectVoList(lqw);
    }

    /**
     * 新增用户消息关联
     * 
     * @param systemUserNoticeDto 用户消息关联
     * @return 执行条数
     */
    @Override
    public int addSystemUserNotice(SysUserNoticeDto systemUserNoticeDto) {
        SysUserNotice systemUserNotice = MapstructUtils.convert(systemUserNoticeDto, SysUserNotice.class);
        return systemUserNoticeMapper.insert(systemUserNotice);
    }

    /**
     * 修改用户消息关联
     * 
     * @param systemUserNoticeDto 用户消息关联
     * @return 执行条数
     */
    @Override
    public int editSystemUserNotice(SysUserNoticeDto systemUserNoticeDto) {
        SysUserNotice systemUserNotice = MapstructUtils.convert(systemUserNoticeDto, SysUserNotice.class);
        return systemUserNoticeMapper.updateById(systemUserNotice);
    }

    /**
     * 批量删除用户消息关联
     * 
     * @param ids 需要删除的用户消息关联ID
     * @return 执行条数
     */
    @Override
    public int removeSystemUserNoticeByIds(Collection<Long> ids) {
        return systemUserNoticeMapper.deleteBatchIds(ids);
    }



}
